package com.cyan.note.util;

import com.cyan.note.security.JwtAuthenticationFilter.CustomAuthenticationDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * Security工具类
 * 用于从Security上下文中获取当前用户信息
 * 
 * @author ZhangzhenYu
 * @since 2025-01-14
 */
public class SecurityUtils {
    
    /**
     * 获取当前认证信息
     */
    public static Authentication getCurrentAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }
    
    /**
     * 获取当前用户名
     */
    public static String getCurrentUsername() {
        Authentication authentication = getCurrentAuthentication();
        if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
            return ((UserDetails) authentication.getPrincipal()).getUsername();
        }
        return null;
    }
    
    /**
     * 获取当前用户ID
     */
    public static Long getCurrentUserId() {
        Authentication authentication = getCurrentAuthentication();
        if (authentication != null && authentication.getDetails() instanceof CustomAuthenticationDetails) {
            return ((CustomAuthenticationDetails) authentication.getDetails()).getUserId();
        }
        return null;
    }
    
    /**
     * 获取当前用户角色
     */
    public static String getCurrentUserRole() {
        Authentication authentication = getCurrentAuthentication();
        if (authentication != null && authentication.getDetails() instanceof CustomAuthenticationDetails) {
            return ((CustomAuthenticationDetails) authentication.getDetails()).getRole();
        }
        return null;
    }
    
    /**
     * 获取当前用户店铺ID
     */
    public static Long getCurrentShopId() {
        Authentication authentication = getCurrentAuthentication();
        if (authentication != null && authentication.getDetails() instanceof CustomAuthenticationDetails) {
            return ((CustomAuthenticationDetails) authentication.getDetails()).getShopId();
        }
        return null;
    }
    
    /**
     * 获取自定义认证详情
     */
    public static CustomAuthenticationDetails getCurrentUserDetails() {
        Authentication authentication = getCurrentAuthentication();
        if (authentication != null && authentication.getDetails() instanceof CustomAuthenticationDetails) {
            return (CustomAuthenticationDetails) authentication.getDetails();
        }
        return null;
    }
    
    /**
     * 检查当前用户是否为超级管理员
     */
    public static boolean isSuperAdmin() {
        return "super_admin".equals(getCurrentUserRole());
    }
    
    /**
     * 检查当前用户是否为店长
     */
    public static boolean isManager() {
        return "manager".equals(getCurrentUserRole());
    }
    
    /**
     * 检查当前用户是否为打手
     */
    public static boolean isWorker() {
        return "worker".equals(getCurrentUserRole());
    }
    
    /**
     * 检查当前用户是否已认证
     */
    public static boolean isAuthenticated() {
        Authentication authentication = getCurrentAuthentication();
        return authentication != null && authentication.isAuthenticated();
    }
}